package com.sky.utils;


import com.sky.config.MinioConfig;
import com.sky.entity.FileInfo;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author xf
 * @Aate 2024/3/4 下午 08:56
 * @Version v1.0.0
 * @Description : TODO: Minio文件操作工具
 */
@Component
public class MinioUtil {

    @Resource
    private MinioConfig minioConfig;

    @Resource
    private MinioClient minioClient;

    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:08
     * @Description：TODO: 创建文件的桶
     * @Param
     */
    public void createBucket(String bucket) throws Exception {
        MinioClient minioClient = minioConfig.getMinioClient();
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!exists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:11
     * @Description：TODO: 上传文件
     * @Param
     */
    public void uploadFile(InputStream inputStream, String bucket, String objectName) throws Exception {
        minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName).
                stream(inputStream, -1, Integer.MAX_VALUE).build());
    }


    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:11
     * @Description：TODO: 列出所有桶
     * @Param
     */
    public List<String> getAllBucket() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:23
     * @Description：TODO: 返回当前桶的所有文件
     * @Param
     */
    public List<FileInfo> getAllFile(String bucket) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException, InvalidResponseException, XmlParserException {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());

        ArrayList<FileInfo> fileInfoList = new ArrayList<>();
        for (Result<Item> result : results) {
            Item item = result.get();
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileName(item.objectName());
            fileInfo.setEtag(item.etag());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfoList.add(fileInfo);
        }

        return fileInfoList;
    }

    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:43
     * @Description：TODO: 下载文件
     * @Param
     */
    public InputStream download(String bucket, String objectName) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:49
     * @Description：TODO: 删除桶
     * @Param
     */
    public void deleteBucket(String bucket) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * @return
     * @author xf
     * @Date 2024/3/4 下午 09:51
     * @Description：TODO: 删除文件
     * @Param
     */
    public void deleteObject(String bucket, String objectName) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
    }



}